<?php
/*********************************************************************\
*  Copyright (c) 1998-2013, TH. All Rights Reserved.
*  Author :qianjin
*  FName  :
*  Time   :2016/03/30 09:57:56
*  Remark :解决，部分发货，并发导致订单状态update失败的
\*********************************************************************/
class b2c_tasks_delivery extends base_task_abstract implements base_interface_task{
    public function exec($params=null){
        $delUpdateModel = app::get('b2c')->model('delivery_update');
        $items = $delUpdateModel->getList('*',array('disabled'=>'false'));
        //将发货单归到订单下，整理数组
        foreach ($items as $key => $value) {
        	$arr[$value['order_id']][$key]=$value['delivery_bn'];
        }
        if(!empty($arr)){
        	unset($items);
        }
        //EC订单明细表
        $itemOrderModel = app::get('b2c')->model('order_items');
        $deliveryModel = app::get('b2c')->model('delivery');
        $delItemModel = app::get('b2c')->model('delivery_items');
        //循环比较，订单商品明细和发货单商品明细。商品明细匹配一致，该变发货单状态
        foreach ($arr as $orderId => $delItem) {
        	$order_items = $itemOrderModel->getList('product_id,bn,sendnum',array('order_id'=>$orderId));
        	foreach ($delItem as $key1 => $delBn) {
        		$delId = $deliveryModel->getList('delivery_id',array('delivery_bn'=>$delBn));
        		$proItem = $delItemModel->getList('product_id,product_bn,number',array('delivery_id'=>$delId[0]['delivery_id']));
        		$proItems[$key1]['product_id'] = $proItem[0]['product_id'];
        		$proItems[$key1]['bn'] = $proItem[0]['product_bn'];
        		$proItems[$key1]['sendnum'] = $proItem[0]['number'];
        	}
	        $proItems=array_reverse($proItems);
	        $result=$this->array_diff_assoc2_deep($order_items,$proItems);
	        $num = '0';
	        //值不为空，就加一
	        foreach ($result as $jian => $value) {
	        	if(!empty($value)){
	        		$num++;
	        	}
	        }//为0，改变该订单状态。
	        if($num == '0'){
	        	$orderModel = app::get('b2c')->model('orders');
	        	$orderModel->update(array('ship_status'=>'1',array('order_id'=>$orderId)));
	        	$delUpdateModel->update(array('disabled'=>true,array('order_id'=>$orderId)));
	        }
        }

    }
	/**
	 * ps ：比较二维数组，返回不同。建要相同
	 * Time：2016/03/30 09:57:07
	 * @author qianjin
	 * @param 参数类型
	 * @return 返回值类型
	*/
    public function array_diff_assoc2_deep($array1, $array2) {
	    $ret = array();
	    foreach ($array1 as $k => $v) {
	        if (!isset($array2[$k])) $ret[$k] = $v;
	        else if (is_array($v) && is_array($array2[$k])) $ret[$k] = $this->array_diff_assoc2_deep($v, $array2[$k]);
	        else if ((string)$v != (string)$array2[$k]) $ret[$k] = $v;
	    }
	    return $ret;
	}

}